﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MedReg
{
    public partial class DocumentType : IEquatable<DocumentType>
    {
        public void ImportList(List<Classes.DocumentType> docTList)
        {
            var mdb = new MedregDBDataContext();
            var ListDocT = new List<DocumentType>();
            foreach (var docT in docTList)
            {
                DocumentType docT1 = new DocumentType() { ID = docT.ID, Name = docT.Name };
                DocumentType query;

                if (mdb.DocumentType.LongCount() == 0)
                {
                    mdb.DocumentType.InsertOnSubmit(docT1);
                }
                else
                {
                    try
                    {
                        query = mdb.DocumentType.Single(l => l.ID == docT1.ID);
                        query.Name = docT1.Name;

                    }
                    catch (ArgumentNullException e)
                    {
                    }
                    catch (InvalidOperationException e)
                    {

                        if (!ListDocT.Contains(docT1))
                        {
                            mdb.DocumentType.InsertOnSubmit(docT1);
                            ListDocT.Add(docT1);
                        }
                    }
                }
            }
            mdb.SubmitChanges();
        }
        public bool Equals(DocumentType other)
        {
            var i = this.ID.Equals(other.ID);
            var j = this.Name.Equals(other.Name);
            if (i & j)
            {
                return true;
            }
            return false;
        }
    }
}
